package it.unibz.easytravel.itemlist;

import it.unibz.easytravel.R;
import it.unibz.easytravel.db.PersonalItemsDbAdapter;
import it.unibz.easytravel.db.SelectPersonalItemsDbAdapter;
import it.unibz.easytravel.expenses.ExpensesList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class TabAddRemovePersonalItems extends Activity {

	private LinearLayout mListCheckbox;
	private PersonalItemsDbAdapter mDbHelper;
	private SelectPersonalItemsDbAdapter mDbSelectHelper;
	//private CheckBoxifiedTextListAdapter cbla;
	// Create CheckBox List Adapter, cbla
	//private String[] items = {"Spoon", "Fork", "Tooth Brush", "Watch", "Hemoroid Cream"};
	// Array of string we want to display in our list
	//private CheckBoxifiedText cbt;
	
	
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
//		setContentView(R.layout.itemlist_browseitems);
//		mDbHelper = new PersonalItemsDbAdapter(this);
//		mDbHelper.open();
//		cbla = new CheckBoxifiedTextListAdapter(this);
//        for(int k=0; k<items.length; k++)
//        {
//        	cbla.addItem(new CheckBoxifiedText(items[k], false));
//        }  
//        // Display it
//        setListAdapter(cbla);
		
		
		mDbSelectHelper = new SelectPersonalItemsDbAdapter(this);
		mDbSelectHelper.open();
		
		mDbHelper = new PersonalItemsDbAdapter(this);
		mDbHelper.open();
		
		setContentView(R.layout.itemlist_addremoveitems);

		Display();
		Button addButton = (Button) findViewById(R.id.Button_ItemListAdd);
		addButton.setOnClickListener(addButtonListener);

		Button removeButton = (Button) findViewById(R.id.Button_ItemListRemove);
		removeButton.setOnClickListener(removeButtonListener);

		Button SelectButton = (Button) findViewById(R.id.Button_ItemListSelect);
		SelectButton.setOnClickListener(selectButtonListener);
		
	}

	// Display all database records on checkboxes
	private void Display() {
		mListCheckbox = (LinearLayout) findViewById(R.id.MenuList_checkbox);

		// Take all records from database
		Cursor cursor = mDbSelectHelper.fetchAllItems();

		// Iterate through all records and put them on checkboxes
		startManagingCursor(cursor);
		if (cursor.moveToFirst()) {
			do {
				CheckBox newCheckBox = new CheckBox(this);
				newCheckBox
						.setText(cursor
								.getString(cursor
										.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_NAME)));
				mListCheckbox.addView(newCheckBox);

			} while (cursor.moveToNext());
		}

	}

	private OnClickListener selectButtonListener = new OnClickListener() {
		public void onClick(View v) {
			Cursor cursor = mDbSelectHelper.fetchAllItems();
			startManagingCursor(cursor);
			cursor.moveToFirst();
			long id, taken = 0;

			String name = "";
			CheckBox item1;
			// remove all items and add only selected
			mDbHelper.delete();
			for (int i = 0; i < mListCheckbox.getChildCount(); i++) {
				item1 = (CheckBox) mListCheckbox.getChildAt(i);
				taken = cursor
						.getInt(cursor
								.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_TAKEN));

				if (item1.isChecked()) {
					//item1.setChecked(false);
					id = cursor
							.getLong(cursor
									.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_ROWID));
					name = cursor
							.getString(cursor
									.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_NAME));

					// status taken 1
					mDbHelper.createItem(name, 0);
				}
				cursor.moveToNext();
			}
			
			Toast.makeText(getBaseContext(),
					"Items selected", Toast.LENGTH_SHORT)
					.show();
		
		}
	};
	// add new item 
	private OnClickListener addButtonListener = new OnClickListener() {
		public void onClick(View v) {
			EditText addValue = (EditText) findViewById(R.id.AddText);
			if (!addValue.getText().toString().equals("")) {
				mDbSelectHelper.createItem(addValue.getText().toString(), 0);
				mListCheckbox.removeAllViews();
				Display();
				addValue.setText("");
			}

		}
	};

	// remove item from database
	private OnClickListener removeButtonListener = new OnClickListener() {
		public void onClick(View v) {
			Cursor cursor = mDbSelectHelper.fetchAllItems();
			startManagingCursor(cursor);
			cursor.moveToFirst();
			long id = 0;
			CheckBox item1;
			for (int i = 0; i < mListCheckbox.getChildCount(); i++) {
				item1 = (CheckBox) mListCheckbox.getChildAt(i);
				if (item1.isChecked()) {
					item1.setChecked(false);
					id = cursor
							.getLong(cursor
									.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_ROWID));
					mDbSelectHelper.deleteItem(id);
				}
				cursor.moveToNext();
			}
			mListCheckbox.removeAllViews();
			Display();
		}
	};
}
